Arrays

  • What is an array
  • Declaring and initializing
  • Accessing

Arrays


For Simple variable or single or scalar variable we store 1 single value at a certain address

Note: for our discussion we take 2 bytes per integer (it may be 4 b

int x = address 200 to 201, (if assume 2 bytes per integer)

An array store multiple (e.g 5) will store data of same type.

int x[5] = address 200 - 209 = 10 bytes (if we assume 2 bytes per integer)

Differentiate values with the use of index, A[1]

Picture

Declaration


arraydeclare.png

With only declaration, then empty array will have garbage values.

Can only initialise a few values or single value, rest will be initialised with zero

int A[5]={1,2};

int A[5]={0}

Don't have to give a size, it will get size from count of values:

int A[]={1,2,3,4,5}

Traverse an Array

int A[5]={1,2,3,4,5}

int *p=&A;

for (int i; i<4; i++){
    cout<<A[i]<<endl
}

or 

for (int i; i<5; i++){
    cout<<i[A]<<endl
}

or use pointer

for (int i; i<5; i++){
    cout<<*p[i]<<endl
}

Static v Dynamic Array

Static means the size is fixed, or Dynmaic means the size is not fixed

By default array is located or created in stack.

To create a dynamic array, we need to create a pointer with new keyword.


int A[5];

int p*;

p=new int [5]

The second array has nothing to do with A array.

The pointer is declare in STACK, but the array is created in HEAP

But we must delete (deallocated) or release memory when not required


delete []p
p = nullptr

Initialize a pointer array in HEAP

int *p=new int[5]()

Otherwise there will be garbage values

How to access array created in HEAP

p[2]=7;

So the pointer acts as NAME for the array. There is no other name for the array.

To increase the size of array?

Increase/Decease size of array in HEAP

How to we do this.

we cannot increase the existing array. But we can create a new pointer to new array with bigger size. Move all the contents of the old array to the new array. Delete old pointer to array. set the new pointer to old pointer of bigger array.

The main idea: Create a new HEAP array pointer, and then point the old array to this new array.

Remember we cannot increase the size of existing array.

arraysize.png

Additional picture

arraysize2.png

In [1]:
#include <iostream>
#include <climits>
#include <math.h>
using namespace std;

Declare both pointer arrays

In [2]:
int *p=new int[5]();
int *q=new int [10]();
In [3]:
cout<<&p<<endl;
cout<<&q<<endl;
0x7fbce67a3028
0x7fbce67a3030

Initialize p pointer values

In [4]:
p[0]=4;
p[1]=45;
p[2]=43;
p[3]=12;
p[4]=7;

Move p pointer values to bigger q pointer array

In [5]:
for (int i=0; i<5; i++){
    q[i]=p[i];
}

Fill positions 6[element 5] and 7[element 6]

In [6]:
q[5]=19;
q[6]=71;

Check the contents of q and see the values are correct

In [7]:
for (int i=0; i<10; i++){
    cout<<q[i]<<endl;
}
4
45
43
12
7
19
71
0
0
0

Delete p pointer array

Seeing that we want to keep the name "p" as out pointer name, we need to remove this pointer to the array of size 5.

So we deleting the array of size 5 that was created in the memory.

In [8]:
delete []p;

Assign q to p

Remember q awas pointing to an array of size 10 in heap, and p was pointing to an array of size 5 in HEAP.

We now point p to array of 10 in HEAP, where q is also pointing to.

In [9]:
p=q;
In [10]:
cout<<&p<<endl;
cout<<&q<<endl;
0x7fbce67a3028
0x7fbce67a3030

Set q to nullptr

This is now to remove q reference to array of size 10 in HEAP, as p is also pointing to it.;

Remember we CANNOT cannot delete the q pointer to array of size 10, that is delete []q is p is pointing to it.

And p is pointing to the SAME array in HEAP, as q

What we can do is to remove q's reference to to this memory HEAP

q = nullptr;
In [11]:
q = nullptr;
In [12]:
cout<<&p<<endl;
cout<<&q<<endl;
0x7fbce67a3028
0x7fbce67a3030

Check the contents of p and see the values are correct

In [13]:
for (int i=0; i<10; i++){
    cout<<p[i]<<endl;
}
4
45
43
12
7
19
71
0
0
0
In [ ]: